const Model = require('../models/hanziStructureModel')

const getAllHanziStructures = async (ctx) => {
  ctx.body = { success: true, data: await Model.getAllHanziStructures() }
}

const getHanziStructureById = async (ctx) => {
  const { id } = ctx.params
  const hanziStructure = await Model.getHanziStructureById(id)
  if (!hanziStructure) ctx.throw(404, 'Hanzi Structure not found')
  ctx.body = hanziStructure
}

const createHanziStructure = async (ctx) => {
  const structureData = ctx.request.body
  const structureId = await Model.createHanziStructure(structureData)
  ctx.status = 201
  ctx.body = { id: structureId, ...structureData }
}

const updateHanziStructure = async (ctx) => {
  const { id } = ctx.params
  const structureData = ctx.request.body
  await Model.updateHanziStructure(id, structureData)
  ctx.body = { success: true }
}

const deleteHanziStructure = async (ctx) => {
  const { id } = ctx.params
  await Model.deleteHanziStructure(id)
  ctx.status = 204
}

module.exports = {
  getAllHanziStructures,
  getHanziStructureById,
  createHanziStructure,
  updateHanziStructure,
  deleteHanziStructure,
}